﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using KDS.Mes.Mes.AlcCodeDetails.Dtos;
using KDS.Mes.Authorization;
using KDS.Mes.Dto;

namespace KDS.Mes.Mes.AlcCodeDetails
{
    /// <summary>
    /// ALCCode信息服务实现
    /// </summary>
    [AbpAuthorize(AppPermissions.Pages_Bom_AlcCodeDetails)]
    public class AlcCodeDetailAppService : MesAppServiceBase, IAlcCodeDetailAppService
    {
        private readonly IRepository<AlcCodeDetail, int> _alcCodeDetailRepository;
        private readonly IAlcCodeDetailListExcelExporter _alcCodeDetailListExcelExporter;


        private readonly AlcCodeDetailManage _alcCodeDetailManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public AlcCodeDetailAppService(IRepository<AlcCodeDetail, int> alcCodeDetailRepository,
AlcCodeDetailManage alcCodeDetailManage
      , IAlcCodeDetailListExcelExporter alcCodeDetailListExcelExporter
  )
        {
            _alcCodeDetailRepository = alcCodeDetailRepository;
            _alcCodeDetailManage = alcCodeDetailManage;
            _alcCodeDetailListExcelExporter = alcCodeDetailListExcelExporter;
        }

        #region ALCCode信息管理

        /// <summary>
        /// 根据查询条件获取ALCCode信息分页列表
        /// </summary>
        public async Task<PagedResultDto<AlcCodeDetailListDto>> GetPagedAlcCodeDetailsAsync(GetAlcCodeDetailInput input)
        {

            var query = _alcCodeDetailRepository.GetAll();
            //TODO:根据传入的参数添加过滤条件

            var alcCodeDetailCount = await query.CountAsync();

            var alcCodeDetails = await query
            .OrderBy(input.Sorting)
            .PageBy(input)
            .ToListAsync();

            var alcCodeDetailListDtos = alcCodeDetails.MapTo<List<AlcCodeDetailListDto>>();
            return new PagedResultDto<AlcCodeDetailListDto>(
            alcCodeDetailCount,
            alcCodeDetailListDtos
            );
        }

        /// <summary>
        /// 通过Id获取ALCCode信息信息进行编辑或修改 
        /// </summary>
        public async Task<GetAlcCodeDetailForEditOutput> GetAlcCodeDetailForEditAsync(NullableIdDto<int> input)
        {
            var output = new GetAlcCodeDetailForEditOutput();

            AlcCodeDetailEditDto alcCodeDetailEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _alcCodeDetailRepository.GetAsync(input.Id.Value);
                alcCodeDetailEditDto = entity.MapTo<AlcCodeDetailEditDto>();
            }
            else
            {
                alcCodeDetailEditDto = new AlcCodeDetailEditDto();
            }

            output.AlcCodeDetail = alcCodeDetailEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取ALCCode信息ListDto信息
        /// </summary>
        public async Task<AlcCodeDetailListDto> GetAlcCodeDetailByIdAsync(EntityDto<int> input)
        {
            var entity = await _alcCodeDetailRepository.GetAsync(input.Id);

            return entity.MapTo<AlcCodeDetailListDto>();
        }







        /// <summary>
        /// 新增或更改ALCCode信息
        /// </summary>
        public async Task CreateOrUpdateAlcCodeDetailAsync(CreateOrUpdateAlcCodeDetailInput input)
        {
            if (input.AlcCodeDetailEditDto.Id.HasValue)
            {
                await UpdateAlcCodeDetailAsync(input.AlcCodeDetailEditDto);
            }
            else
            {
                await CreateAlcCodeDetailAsync(input.AlcCodeDetailEditDto);
            }
        }

        /// <summary>
        /// 新增ALCCode信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_AlcCodeDetails_Create)]
        public virtual async Task<AlcCodeDetailEditDto> CreateAlcCodeDetailAsync(AlcCodeDetailEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<AlcCodeDetail>();

            entity = await _alcCodeDetailRepository.InsertAsync(entity);
            return entity.MapTo<AlcCodeDetailEditDto>();
        }

        /// <summary>
        /// 编辑ALCCode信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_AlcCodeDetails_Edit)]
        public virtual async Task UpdateAlcCodeDetailAsync(AlcCodeDetailEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _alcCodeDetailRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _alcCodeDetailRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除ALCCode信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_AlcCodeDetails_Delete)]
        public async Task DeleteAlcCodeDetailAsync(EntityDto<int> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _alcCodeDetailRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除ALCCode信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_Bom_AlcCodeDetails_Delete)]
        public async Task BatchDeleteAlcCodeDetailAsync(List<int> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _alcCodeDetailRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion


        #region ALCCode信息的Excel导出功能


        public async Task<FileDto> GetAlcCodeDetailToExcel()
        {
            var entities = await _alcCodeDetailRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<AlcCodeDetailListDto>>();

            var fileDto = _alcCodeDetailListExcelExporter.ExportAlcCodeDetailToFile(dtos);



            return fileDto;
        }


        #endregion










    }
}
